package com.xindacloud.sms.portal.controller;


import com.xindacloud.sms.portal.dto.PricingDTO;
import com.xindacloud.sms.portal.service.ISmsUserAllowOperatorService;
import com.xindacloud.sms.portal.utils.CsvExportUtil;
import com.xindacloud.sms.portal.utils.UserSessionHelp;
import com.xindacloud.sms.portal.vo.PricingListVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Api(tags = {"价格接口"})
@RestController
@RequestMapping(value = "/api/v1/portal/pricing")
@Slf4j
public class PricingController {


    final ISmsUserAllowOperatorService userAllowOperatorService;

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    public PricingController(ISmsUserAllowOperatorService userAllowOperatorService) {
        this.userAllowOperatorService = userAllowOperatorService;
    }

    @ApiOperation(value = "查询价格列表", response = PricingListVO.class)
    @PostMapping(value = "/list", produces = MediaType.APPLICATION_JSON_VALUE)
    public Object queryPricingList(@ApiParam(name = "pricingDTO", value = "pricingDTO", required = true)
                                   @RequestBody PricingDTO pricingDTO,
                                   HttpServletRequest request) {
        pricingDTO.setUserId(UserSessionHelp.getUserFromSession(request).getUserId());
        return userAllowOperatorService.queryUserPricingPage(pricingDTO);
    }

    @ApiOperation(value = "下载价格")
    @PostMapping(value = "/download")
    public void downloadPricing(HttpServletRequest request,HttpServletResponse response) {

        try {
            PricingDTO pricingDTO = new PricingDTO();
            pricingDTO.setUserId(UserSessionHelp.getUserFromSession(request).getUserId());
            // 查询需要导出的数据
            List<PricingListVO> pricingListVOList = userAllowOperatorService.downloadPricing(pricingDTO);

            // 构造导出数据结构
            String titles = "countryCode,countryName,networkCode,networkName,prefix,price,currency";  // 设置表头
            String keys = "countryCode,countryName,networkCode,networkName,prefix,price,currency";  // 设置每列字段

            // 构造导出数据
            List<Map<String, Object>> dataList = new ArrayList<>();
            Map<String, Object> map;

            for (PricingListVO price : pricingListVOList) {
                final String countryName = price.getCountryName();
                final String networkCode = price.getNetworkCode();
                final String networkName = price.getNetworkName();
                final String prefix = price.getPrefix();
                map = new HashMap<>();
                map.put("countryCode", replaceStr(price.getCountryCode()));
                map.put("countryName", replaceStr(countryName));
                map.put("networkCode", replaceStr(networkCode));
                map.put("networkName", replaceStr(networkName));
                map.put("prefix", replaceStr(prefix));
                map.put("price", String.format("%.4f", price.getPrice()));
                map.put("currency", replaceStr(price.getCurrency()));
                dataList.add(map);
            }

            // 设置导出文件前缀
            String fName = "pricing-";
            // 文件导出
            OutputStream os = response.getOutputStream();
            CsvExportUtil.responseSetProperties(fName, "csv", response);
            CsvExportUtil.doExport(dataList, titles, keys, os);
            os.close();
        } catch (Exception e) {
            log.error("导出失败" + e.getMessage(), e);
        }
    }

    private String replaceStr(String str) {
        return StringUtils.isNotEmpty(str) ? str.replace(",", " ") : "UNKNOWN";
    }


    /*@ApiOperation(value = "下载Excel")
    @GetMapping(value = "/download-excel", produces = MediaType.APPLICATION_JSON_VALUE)
    public void downloadExcel(HttpServletResponse response) {

        try {
            PricingDTO pricingDTO = new PricingDTO();
            // 查询需要导出的数据
            List<PricingListVO> pricingListVOList = smsPricingService.downloadPricing(pricingDTO);

            String titles = "countryCode,countryName,networkCode,networkName,prefix,currency,price";  // 设置表头
            String clounms[]=titles.split(",");

            // 设置导出文件前缀
            String fName = "pricing-";
            // 文件导出
            OutputStream os = response.getOutputStream();
            ExcelExportUtil.responseSetProperties(fName,"xlsx", response);
            ExcelExportUtil.exportExcel(clounms,pricingListVOList, "pricing", os);
            os.close();
        } catch (Exception e) {
            log.error("导出失败" + e.getMessage(), e);
        }
    }*/
}
